package edu.gmu.projects.fireresponder.notificationhandler;

import java.util.ArrayList;
import java.util.List;

import edu.gmu.projects.fireresponder.FirstResponder;
import edu.gmu.projects.fireresponder.environment.Asset;
import edu.gmu.projects.fireresponder.environment.Building;
import edu.gmu.projects.fireresponder.environment.Path;
import edu.gmu.projects.fireresponder.environment.Route;
import edu.gmu.projects.fireresponder.environment.Safespot;
import edu.gmu.projects.fireresponder.fusionengine.FusionEngine;
import edu.gmu.projects.fireresponder.relevancyengine.IRelevanceEngineListener;
import edu.gmu.projects.fireresponder.relevancyengine.RelevanceEngine;
import edu.gmu.projects.fireresponder.relevancyengine.RelevanceEngine.RelevanceScale;
import edu.gmu.projects.fireresponder.xml.alerts.FirstResponderAlert;

public class NotificationHandler implements IRelevanceEngineListener {

	private List<INotificationHandlerListener> nListeners = new ArrayList<INotificationHandlerListener>();
	private List<IMapUpdateListener> mListeners = new ArrayList<IMapUpdateListener>();

	public void addNotificationListener(INotificationHandlerListener listener) {
		nListeners.add(listener);
	}

	public void removeNotificationListener(INotificationHandlerListener listener) {
		nListeners.remove(listener);
	}

	public List<INotificationHandlerListener> getNotificationListeners() {
		return new ArrayList<INotificationHandlerListener>(nListeners);
	}

	public void addMapListener(IMapUpdateListener listener) {
		mListeners.add(listener);
	}

	public void removeMapListener(IMapUpdateListener listener) {
		mListeners.remove(listener);
	}

	public List<IMapUpdateListener> getMapListeners() {
		return new ArrayList<IMapUpdateListener>(mListeners);
	}

	public void onPeopleInBuildingUpdate(Building asset) {
		// Icon changes
		for (IMapUpdateListener l : mListeners) {
			l.displayPeopleInBuilding(asset);
		}
	}

	public void onNoPeopleInBuildingUpdate(Building asset) {
		// Icon change
		for (IMapUpdateListener l : mListeners) {
			l.removePeopleInBuildingDisplay(asset);
		}
	}
	
	public void onFireProbabilityUpdated(Asset asset) {
		// Level 1 - Icon changes
		for (IMapUpdateListener l : mListeners) {
			if (asset instanceof Building) {
				l.displayBuildingOnFire((Building) asset);
			} else if (asset instanceof Path) {
				l.displayPathOnFire((Path) asset);
			} else if (asset instanceof Safespot) {
				l.displaySafespotOnFire((Safespot) asset);
			} else if (asset instanceof Route) {
				l.displayRouteOnFire((Route) asset);
			}
		}

		// Level 2 - Status bar notification
		RelevanceScale scale = RelevanceEngine.getRelevanceScale(asset
				.getRelevanceValue());

		switch (scale) {
		case MODERATE_RELEVANCE:
		case HIGH_RELEVANCE:
		case VERY_HIGH_RELEVANCE:
			for (INotificationHandlerListener l : nListeners) {
				double probOfFire = asset.getProbabilityOfFire();
				double probPeople = asset.getProbabilityOfPresence();

				String probOfFireText = FusionEngine
						.getProbabilityCategoryText(probOfFire);
				String probPeopleText = FusionEngine
						.getProbabilityCategoryText(probPeople);
				String relevanceText = RelevanceEngine
						.getRelevanceScaleText(scale);

				String message = String
						.format(
								"Fire prob: %s\nPeople prob: %s\nMission Relevance: %s",
								probOfFireText, probPeopleText, relevanceText);

				l.addStatusBarNotification(asset.hashCode(), asset.getId(),
						message);
				l.addFireNotificationToDatabase(asset);
			}
			break;
		}
	}

	public void onCurrentMissionTaskUpdated(Asset asset) {
		String action = "proceed to ";

		if (asset instanceof Path || asset instanceof Route) {
			action += "path";
		} else if (asset instanceof Safespot) {
			action += "safespot";
		}

		String message = String.format("Please %s %s", action, asset.getId());

		for (INotificationHandlerListener l : nListeners) {
			l.showDialogAlert(message);
		}
		
		for (IMapUpdateListener l : mListeners) {
			l.updateMostRelevantAsset(asset);
		}
	}

	public void onLoadingNewMisisonPlan() {
		for (IMapUpdateListener l : mListeners) {
			l.loadingNewMissionPlan();
		}		
	}

	public void onLoadNewMissionPlan(String plan) {
		for (IMapUpdateListener l : mListeners) {
			l.loadNewMissionPlan(plan);
		}

		for (INotificationHandlerListener l : nListeners) {
			l.showDialogAlert("New Mission Plan");
		}
	}

	public void onLoadedNewMissionPlan() {
		for (IMapUpdateListener l : mListeners) {
			l.loadedNewMissionPlan();
		}
	}
	
	public void onMissionCompleted() {
		for (INotificationHandlerListener l : nListeners) {
			l.showDialogAlert("Mission complete");
		}
	}

	public void onBuildingCleared(Building asset) {
		for (IMapUpdateListener l : mListeners) {
			l.displayBuildingCleared(asset);
		}
	}

	public void onBuildingDropped(Building asset) {
		for (IMapUpdateListener l : mListeners) {
			l.displayBuildingDropped(asset);
		}
	}

	public void onTeamMemberUnable(Building asset) {
		for (IMapUpdateListener l : mListeners) {
			l.displayTeamMemberBuildingDropped(asset);
		}
	}

	public void onResponderUnable(Building asset) {
		for (IMapUpdateListener l : mListeners) {
			l.displayBuildingDropped(asset);
		}
	}

	public void onResponderLocationUpdated(FirstResponder asset) {
		for (IMapUpdateListener l : mListeners) {
			l.displayResponderLocation(asset);
		}
	}

	public void onMessageOrImageReceived(FirstResponderAlert alert) {
		for (IMapUpdateListener l : mListeners) {
			l.displayResponderAttachments(alert);
		}
	}
}